﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2018.04.09
 * 描 述：数据列表选择
 */
var dfopid = request('dfopid');
var acceptClick;
var currentLevel = 0;

var bootstrap = function ($, learun) {
    "use strict";

    var op = null;
    var grid = null;

    var selectData = {};
    var renderData = {};


    var page = {
        init: function () {
            page.bind();
            page.search();
        },
        bind: function () {
            op = top.lrlayerSelect[dfopid];

            var _value = op._value.split(',');
            var _text = op._text.split(',');
            $.each(_value, function (_index, _item) {
                if (_item != "") {
                    selectData[_item] = _text[_index] || '';
                }
            });

            // 初始化表格
            grid = $('#gridtable').jfGrid({
                url: op.url,
                headData: op.grid,
                isMultiselect: op.isMultiple,
                mainId: op.dataValueId,
                isPage: true,
                isTree: false,
                multiselectfield: 'lrcheck',
                onSelectRow: function (data, isCheck) {
                    page.setSelect(data, isCheck);
                },
                onRenderComplete: function (rows) {
                    var _data = [];
                    var $list = $('#selected_item_list');
                    for (var i = 0; i < rows.length; i++) {
                        if (!!selectData[rows[i][op.dataValueId]]) {
                            selectData[rows[i][op.dataValueId]] = rows[i][op.dataTextId];
                            renderData[rows[i][op.dataValueId]] = rows[i][op.dataTextId];
                            rows[i].lrcheck = 1;

                            if (renderData[rows[i][op.dataValueId]] == undefined) {
                                var _html = '<div class="item-selected-box" data-value="' + rows[i][op.dataValueId] + '" >';
                                _html += '<p><span>' + rows[i][op.dataTextId] + '</span></p>';
                                _html += '<span class="item-reomve" title="移除选中项"></span>';
                                _html += '</div>';
                                $list.append($(_html));
                            }
                        } else {
                            rows[i].lrcheck = 0;
                        }
                        _data.push(rows[i]);
                    }
                    $('#gridtable').jfGridSet('refreshdata', _data);
                },
            });

            $("#jfgrid_all_cb_gridtable").click(function () {
                var imageurl = '/assets/plugins/images/jfgrid/';
                var cb = ['checkbox_0.png', 'checkbox_1.png', 'checkbox_2.png'];
                var op = $(grid)[0].dfop;
                var $et = $(this);

                // 判断当前是否是选中还是不选中
                var imgsrc = $et.attr('src').replace(imageurl, '');
                var ischecked = false;
                if (imgsrc == cb[1]) {
                    ischecked = true;
                }
                op.running.checkAlling = true;
                $.each(op.running.rowdata, function (_index, _item) {
                    if (_item['jfcheck'].value + '' == '0' && !ischecked) {
                        _item['jfcheck'].$cell.trigger('click');
                    } else if (_item['jfcheck'].value + '' == '1' && ischecked) {
                        _item['jfcheck'].$cell.trigger('click');
                    }
                });
                op.running.checkAlling = false;
            });

            //剔除0余额
            $("#isRejectZero").click(function () {
                var rejectZero;
                if ($(this).is(':checked')) {
                    rejectZero = 1;
                } else {
                    rejectZero = 0;
                }
                page.search({isLeaf: currentLevel, rejectZero: rejectZero});
            });

            // 搜索框初始化
            $('#txt_keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });

            $('input[type=radio][name=isLeaf]').change(function () {
                var level = this.value;
                currentLevel = level;
                var rejectZero;
               if($("#isRejectZero").is(':checked')){
                   rejectZero = 1;
               } else{
                   rejectZero = 0;
               }
                page.search({isLeaf: level, rejectZero: rejectZero});
            });

            $('.input-query').on('click', function () {
                var keyword = $('#txt_keyword').val();
                page.search({keyword: keyword});
            });

            // 已选项
            if (op.isMultiple) {
                $('#item_selected_btn').on('click', function () {
                    $('#form_warp_right').animate({right: '0px'}, 300);
                });
                $('#item_selected_btn_close').on('click', function () {
                    $('#form_warp_right').animate({right: '-180px'}, 300);
                });
            } else {
                $('#item_selected_btn').remove();
                $('.input-query').css('right', '10px');
            }

            $('.selected-item-list-warp').lrscroll();

            var $warp = $('#selected_item_list');
            $warp.on('click', function (e) {
                var et = e.target || e.srcElement;
                var $et = $(et);
                if ($et.hasClass('item-reomve')) {
                    var id = $et.parent().attr('data-value');
                    $et.parent().remove();
                    delete selectData[id];
                    var keyword = $('#txt_keyword').val();
                    page.search({keyword: keyword});
                }
            });

            jQuery.each(selectData, function (i, val) {
                if (renderData[i] == undefined) {
                    var _html = '<div class="item-selected-box" data-value="' + i + '" >';
                    _html += '<p><span>' + val + '</span></p>';
                    _html += '<span class="item-reomve" title="移除选中项"></span>';
                    _html += '</div>';
                    $warp.append($(_html));
                    renderData[i] = val;
                }
            });
        },

        search: function (param) {
            param = param || {};
            $('#gridtable').jfGridSet('reload', param);
        },
        setSelect: function (data, ischeck) {
            var $list = $('#selected_item_list');
            if (ischeck) {
                if ($list.find('[data-value="' + data[op.dataValueId] + '"]').length == 0) {
                    selectData[data[op.dataValueId]] = data[op.dataTextId];
                    var _html = '<div class="item-selected-box" data-value="' + data[op.dataValueId] + '" >';
                    _html += '<p><span>' + data[op.dataTextId] + '</span></p>';
                    _html += '<span class="item-reomve" title="移除选中项"></span>';
                    _html += '</div>';
                    $list.append($(_html));
                }
            } else {
                $list.find('[data-value="' + data[op.dataValueId] + '"]').remove();
                delete selectData[data[op.dataValueId]];
            }

        }

    };
    // 保存数据
    acceptClick = function (callBack) {
        if (op.isMultiple) {
            callBack(selectData, dfopid);
        } else {
            var item = $('#gridtable').jfGridGet('rowdata');
            selectData = {};
            if (item) {
                selectData[item[op.dataValueId]] = item[op.dataTextId];
            }
            callBack(selectData, dfopid);
        }
        return true;
    };
    page.init();
}